<?php
/**
 * Created by JetBrains PhpStorm.
 * User: shaoshuai
 * Date: 12-7-14
 * Time: 下午10:53
 * To change this template use File | Settings | File Templates.
 */
class Matrix
{
    /**
     * @static
     * @param array $matrixA
     * @param array $matrixB
     * @param array $coordinate
     */
    public static function add(&$matrixA, $matrixB, $coordinate)
    {
        $aWidth = count($matrixA[0]);
        $aHeight = count($matrixA);
        $bWidth = count($matrixB[0]);
        $bHeight = count($matrixB);

        for ($y = $coordinate['y'], $i = 0; $i < $bHeight && $y < $aHeight; $y++, $i++) {
            for ($x = $coordinate['x'], $j = 0; $j < $bWidth && $x < $aWidth; $x++, $j++) {
                $matrixA[$y][$x] += $matrixB[$i][$j];
            }
        }
    }

    public static function test()
    {
        $a = array(
            array(0, 0, 0, 0),
            array(0, 0, 0, 0),
            array(0, 0, 0, 0),
            array(0, 0, 0, 0),
        );

        $b = array(
            array(0, 1),
            array(1, 1),
        );

        Matrix::add($a, $b, array('x' => 3, 'y' => 1));

        print_r($a);
    }
}

//Matrix::test();

